package com.venusj.sharding.hint;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author zhangjh
 * @date 2021/2/3
 * @desc
 */
public class MyHintShardingAlgorithm implements HintShardingAlgorithm<Long> {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Long> shardingValue) {
        // 添加分库或分表路由逻辑
        Collection<String> result = new ArrayList<>();
        for(String each : availableTargetNames)
        {
            for(Long value : shardingValue.getValues())
            {
                if(each.endsWith(String.valueOf(value % 2)))
                {
                    result.add(each);
                }
            }
        }
        return result;
    }
}
